package com.example.wallpaperapp.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.wallpaperapp.R;
import com.example.wallpaperapp.bean.HomeImageRecyclerViewBean;
import com.example.wallpaperapp.widget.RoundImageView;

import java.util.List;

public class NowuseRecyclerViewAdapter extends RecyclerView.Adapter< NowuseRecyclerViewAdapter.ViewHolder> {
    private MyOnItemClickListener myOnItemClcikListener;
    private Context mContext;
    private MyOnLongClickListener myOnLongClickListener;
    private List<HomeImageRecyclerViewBean> lists;
    public NowuseRecyclerViewAdapter(Context context, List<HomeImageRecyclerViewBean> lists) {
        this.lists = lists;
        mContext = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_home_recyclerviews,parent,false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
          HomeImageRecyclerViewBean bean = lists.get(position);
//          holder.image.setImageResource(bean.getImageId());//getImage_url
        if (myOnItemClcikListener != null && !holder.image.hasOnClickListeners()) {
            holder.image.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //回调方法,,将view控件和索引传回,索引必须是布局在父布局中的索引,否则索引错乱最终报错
                    myOnItemClcikListener.myOnItemClickListener(holder.getLayoutPosition(), v);
                }
            });
        }
        Glide.with(mContext).load(bean.getImage_url()).into(holder.image);
    }
    @Override
    public int getItemCount() {
        return lists.size();
    }
    class ViewHolder extends RecyclerView.ViewHolder {
          RoundImageView image;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            image = itemView.findViewById(R.id.home_image_recyclers);
        }
    }
    //对外暴露的方法,当设置该方法时,会创建接口的实现类
    public void setOnItemCickListener(MyOnItemClickListener myOnItemClickListener) {
        this.myOnItemClcikListener = myOnItemClickListener;
    }

    public void setOnLongCickListener(MyOnLongClickListener myOnLongClickListener) {
        this.myOnLongClickListener = myOnLongClickListener;
    }

    //自定义点击接口
    public interface MyOnItemClickListener {
        void myOnItemClickListener(int position, View view);
    }

    //自定义长按接口
    public interface MyOnLongClickListener {
        void myOnLongClickListener(int position, View view);
    }
}
